package cn.funeralobjects.ams.web.utils;

import cn.funeralobjects.ams.web.api.ProductApi;
import cn.funeralobjects.ams.web.api.ProductApiApi;
import cn.funeralobjects.ams.web.model.MProduct;
import cn.funeralobjects.ams.web.model.MProductApi;
import cn.funeralobjects.ams.web.model.VProduct;
import cn.funeralobjects.ams.web.model.VProductApi;
import cn.funeralobjects.utils.aliyun.core.AliyunClientFactory;
import cn.funeralobjects.utils.aliyun.core.model.*;
import com.aliyuncs.exceptions.ClientException;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * Api的的快速处理工具
 *
 * @author FuneralObjects
 * Create date: 2020/6/1 6:53 AM
 */
public class ApiUtils {

    /**
     * 创建访问客户端
     *
     * @param prodResCode   产品资源编码
     * @param aliyunAction  阿里云API信息
     * @param productApiApi 访问接口对象
     * @return 阿里云客户端
     */
    public static Mono<AliyunClient> createClient(String prodResCode, AliyunAction aliyunAction, ProductApiApi productApiApi) {
        return productApiApi.getExistsAccessKey(prodResCode, aliyunAction.getAction(), aliyunAction.getVersion())
                .map(
                        vAccessKey -> AliyunClientFactory
                                .builder().setAccessKeyId(vAccessKey.getAccessKeyId())
                                .setAccessKeySecret(vAccessKey.getAccessKeySecret())
                                .build(vAccessKey.getRegionId())
                                .buildClient(aliyunAction)
                );
    }

    public static <T extends BaseAliyunData> T doAction(AliyunClient client, BaseAliyunParam<T> param) {
        try {
            return client.doAction(param).getData();
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 添加产品
     *
     * @param product    产品信息
     * @param productApi 产品接口
     * @return 添加到系统中的数据
     */
    public static Mono<VProduct> addProductIfNotExists(AliyunProduct product, ProductApi productApi) {
        return productApi.findByCode(product.code())
                .filter(Objects::nonNull)
                .switchIfEmpty(productApi.add(new MProduct().setCode(product.code()).setDescription(product.description())));
    }

    /**
     * 添加产品的对应API
     *
     * @param aliyunAction  阿里云API信息
     * @param productApiApi 产品API接口
     * @param productId     对应的产品id
     * @return 添加到系统中的数据
     */
    public static Mono<VProductApi> addProductApiIfNotExists(AliyunAction aliyunAction, ProductApiApi productApiApi, Integer productId) {
        return productApiApi.findByActionAndVersionInProduct(productId, aliyunAction.getAction(), aliyunAction.getVersion())
                .filter(Objects::nonNull)
                .switchIfEmpty(productApiApi.add(new MProductApi()
                        .setAction(aliyunAction.getAction())
                        .setMethod(aliyunAction.getMethod())
                        .setDomain(aliyunAction.getDomain())
                        .setProtocol(aliyunAction.getProtocol())
                        .setVersion(aliyunAction.getVersion())
                        .setProductId(productId)
                        .setName(aliyunAction.getName())
                ));
    }
}
